
Imports cmplib01


Public Class frmAdvantage
    Private ptrAdv As Advantage

    Private sVal() As Integer
    Private sName() As String

    Public Shadows Sub showdialog(ByVal value As Advantage)
        ptrAdv = value
        PopulateContracts()
        PopulateStatusBox()
        Populate()
        MyBase.ShowDialog()
    End Sub

    Private Sub PopulateContracts()
        Try

            cboExpiry.Items.Add(New NonExpiringContract)
            cboExpiry.Items.Add(New StatusExpiringContract)
            cboExpiry.Items.Add(New TurnNumberExpiringContract)
            cboExpiry.Items.Add(New BattleExpiringContract)

            cboExpiry.SelectedIndex = 0

        Catch ex As Exception
            HandleException("frmAdvantage", ex)
        End Try
    End Sub

    Private Sub PopulateStatusBox()
        Try
            sVal = [Enum].GetValues(GetType(cmplib01.GroundUnitStatus_cgd))
            sName = [Enum].GetNames(GetType(cmplib01.GroundUnitStatus_cgd))

            cboStatus.Items.Clear()

            For Each s As String In sName
                cboStatus.Items.Add(s)
            Next

            cboStatus.SelectedIndex = 0

        Catch ex As Exception

        End Try
    End Sub

    Private Sub Populate()
        Try
            txtName.Text = ptrAdv.name
            txtDescription.Text = ptrAdv.description

            '// determine which contract we're using
            Select Case ptrAdv.contract.ExpirationContract
                Case ExpirationContractType.never_expires
                    cboExpiry.SelectedIndex = 0

                Case ExpirationContractType.status_expire
                    cboExpiry.SelectedIndex = 1
                    '// get the exp details
                    Dim staExp As StatusExpiringContract = ptrAdv.contract
                    Dim i As Integer = GetIndexFromEnumName(staExp.requiredStatus)
                    cboStatus.SelectedIndex = i

                Case ExpirationContractType.number_of_turns
                    cboExpiry.SelectedIndex = 2
                    Dim numExp As TurnNumberExpiringContract = ptrAdv.contract
                    numTurns.Value = numExp.numberTurns

                Case ExpirationContractType.battle_expire
                    cboExpiry.SelectedIndex = 3
                    Dim battExp As BattleExpiringContract = ptrAdv.contract
                    optWin.Checked = True

                Case Else
                    cboExpiry.SelectedIndex = 0
            End Select

        Catch ex As Exception
            txtName.Text = "Error getting advantage"
        End Try
    End Sub

    Private Function GetIndexFromEnumName(ByVal sName As String) As Integer
        Try
            Dim i As Integer = 0

            For Each s As String In sName
                If s.Equals(sName) Then
                    Return i
                End If
                i += 1
            Next
        Catch ex As Exception

        End Try
    End Function

    Private Sub cboExpiry_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboExpiry.SelectedIndexChanged
        '// hide all panels
        pnlStatusExpiration.Visible = False
        pnlTurnExpiration.Visible = False
        pnlBattleExpiry.Visible = False

        If cboExpiry.SelectedItem Is Nothing Then Exit Sub

        Try
            Dim exp As IExpirationContract = cboExpiry.SelectedItem

            Select Case exp.ExpirationContract
                Case ExpirationContractType.never_expires

                Case ExpirationContractType.status_expire
                    pnlStatusExpiration.Visible = True

                Case ExpirationContractType.number_of_turns
                    pnlTurnExpiration.Visible = True
                Case ExpirationContractType.battle_expire
                    pnlBattleExpiry.Visible = True
            End Select

        Catch ex As Exception
            HandleException("frmAdvantage", ex)
        End Try
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        UpdateAdvantage()
        Me.Hide()
    End Sub

    Private Sub UpdateAdvantage()
        Try
            ptrAdv.name = Me.txtName.Text
            ptrAdv.description = txtDescription.Text

            '// determine what kind of contract is selected
            Select Case cboExpiry.SelectedIndex
                Case 0
                    '// manual expiration
                    ptrAdv.contract = New NonExpiringContract

                Case 1
                    Dim stat As New StatusExpiringContract
                    stat.requiredStatus = sVal(cboStatus.SelectedIndex)
                    ptrAdv.contract = stat

                Case 2
                    Dim numb As New TurnNumberExpiringContract
                    numb.numberTurns = numTurns.Value
                    ptrAdv.contract = numb

                Case 3
                    Dim batt As New BattleExpiringContract
                    If optWin.Checked Then
                        batt.expiresAfterLoss = False
                    Else
                        batt.expiresAfterLoss = True
                    End If
                    ptrAdv.contract = batt

            End Select
        Catch ex As Exception
            HandleException("frmAdvantage", ex)
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Hide()
    End Sub
End Class